package doubleLinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tao
 * Date: 2024-01-01
 * Time: 13:04
 */
public class MyLinkedList implements Ilist{
    public ListNode head;//头结点
    public ListNode last;//尾结点

    static class ListNode {
        int val;
        ListNode next;
        ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }

    @Override
    public void addFirst(int data) {
        ListNode cur = new ListNode(data);
        if(this.head == null) {
            this.head = cur;
            this.last = cur;
        } else {
            cur.next = this.head;
            this.head.prev = cur;
            this.head = cur;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode cur = new ListNode(data);
        if(last == null) {
            head = cur;
            last = cur;
        } else {
            last.next = cur;
            cur.prev = last;
            last = cur;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        //检查下标是否合法
        if(index < 0 || index > size()) {
            throw new IndexException("下标不合法");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = new ListNode(data);
        ListNode prev = this.head;
        int count = 0;
        while (count < index - 1) {
            prev = prev.next;
            count++;
        }
        ListNode prevNext = prev.next;
        prev.next = cur;
        cur.prev = prev;
        cur.next = prevNext;
        prevNext.prev = cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            if(head != null) {
                head.prev = null;
            } else {
                last = null;
                return;
            }
        }
        ListNode prev = findPrev(key);
        if(prev == null) {
            //没有要删的元素
            return;
        }
        ListNode cur = prev.next;
        if(cur.next != null) {
            prev.next = cur.next;
            cur.next.prev = cur.prev;
        } else {
            //最后一个元素
            prev.next = cur.next;//null
            last = prev;
        }
    }

    private ListNode findPrev(int key) {
        ListNode cur = this.head;
        ListNode curNext = cur.next;
        while (curNext != null) {
            if(curNext.val != key) {
                cur = cur.next;
                curNext = curNext.next;
            } else {
                return cur;
            }
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur != null) {
            if(cur.val == key) {
                if(cur.next != null) {
                    prev.next = cur.next;
                    cur.next.prev = prev;
                } else {
                    prev.next = cur.next;//null
                    last = prev;
                }
                cur = cur.next;
            } else {
                prev = prev.next;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
            if(head != null) {
                head.prev = null;
            }
        }
    }

    @Override
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
